package com.acegear.horizon.domain.models.vo;

import com.acegear.horizon.ApplicationContextProvider;
import com.acegear.horizon.domain.models.Post;
import com.acegear.horizon.domain.models.constraint.DailyFeedType;
import com.acegear.horizon.domain.models.jpa.UserPost;
import com.acegear.horizon.domain.repository.PostRepository;

import java.util.Optional;

/**
 * Created by wangsike on 2017/2/25.
 */
public class DailyPostVO extends DailyFeedVO<UserPostVO> {
    private PostRepository postRepository;


    public DailyPostVO(Long viewerId, Long feedId) {
        super(viewerId, DailyFeedType.POST, feedId);
        init();

        Optional<Post> optional = postRepository.findByPostId(feedId);
        if (optional.isPresent()) {
            UserPost userPost = optional.get().getUserPost();
            if (userPost != null && !userPost.isDeleted()) {
                setBody(optional.get().getUserPostVO(viewerId));
            }
        }
    }

    public void init() {
        postRepository = ApplicationContextProvider.getBean(PostRepository.class);
    }
}
